<?php
//categories - functions
function cat_add() {
	if(!perms_check('categories', 'add')) {
		redirect('index.php?module=error&error=auth_error');
		exit();
	}

	global $conf, $lang;

	SQLvalidate($_POST['parent'], 'int', 6);

	//checking if name contains / or \
	$languages = get_lang_list();
	foreach($languages as $l) {
		SQLvalidate($_POST["desc_$l"], 'varchar');
		SQLvalidate($_POST["name_$l"], 'varchar');
		$_POST["desc_$l"]=post_text($_POST["desc_$l"]);
		$_POST["name_$l"]=post_text($_POST["name_$l"]);
	}
	//

	foreach($languages as $l){
		$sql1 .= ", '".$_POST["name_$l"]."', '".$_POST["desc_$l"]."'";
		$sql2 .= ", `name_".$l."`, `desc_".$l."`";
	}

	$db = new dbquery;
	$query = "INSERT INTO $conf[prefix]categories (`id`, `parent`".$sql2.") VALUES (NULL, $_POST[parent]".$sql1.")";
	$db->query($query) or $db->err(__FILE__, __LINE__);

	if($_POST['parent'] == 0)
	
	else
	

	$db->clear_cache('subcats_'.$_POST['parent'].'_*.cache', 'categories');
	$db->clear_cache('subcats_all_'.$_POST['parent'].'_*.cache', 'categories');
	$db->clear_cache('cat_menu_list_*_'.$_POST['parent'].'_*.cache', 'categories');
	$db->clear_cache('cat_menu_list_num_rows_*_'.$_POST['parent'].'_*.cache', 'categories');
	$db->clear_cache('cat_get_link_'.$_POST['parent'].'.cache', 'categories');
	$db->clear_cache('cat_'.$_POST['parent'].'.cache', 'categories');

	redirect('index.php?module=admin&action=categories');
	exit();
}

function cat_edit() {
	if(!perms_check('categories', 'edit')) {
		redirect('index.php?module=error&error=auth_error');
		exit();
	}

	global $conf, $lang;

	SQLvalidate($_POST['id'], 'int', 6);
	SQLvalidate($_POST['parent'], 'int', 6);

	//checking if name contains / or \
	$languages = get_lang_list();
	foreach($languages as $l) {
		SQLvalidate($_POST['desc_'.$l], 'varchar');
		SQLvalidate($_POST['name_'.$l], 'varchar');
		$_POST["desc_$l"]=post_text($_POST["desc_$l"]);
		$_POST["name_$l"]=post_text($_POST["name_$l"]);
	}
	//

	$db = new dbquery;
	$db->query("SELECT * FROM $conf[prefix]categories WHERE id = $_POST[id]") or $db->err(__FILE__, __LINE__);
	$d = $db->fetch_object();

	if($_POST['parent'] != $d->parent) {
		$xxx = true;
		if($d->parent != 0)
		cat_update_values($_POST['id'], 0);
	}
	else
	$xxx = false;

	$query = "UPDATE $conf[prefix]categories SET parent = $_POST[parent]";

	foreach($languages as $l)
	$query .= ", name_$l = '".$_POST['name_'.$l]."', desc_$l = '".$_POST['desc_'.$l]."'";

	$query .= " WHERE id = $_POST[id]";

	$db->query($query) or $db->err(__FILE__, __LINE__);

	if($xxx === true)
	cat_update_values($_POST['id'], 1);

	//adding log
	
	//

	//czyszczenie cache
	$db->clear_cache('subcats_'.$_POST['parent'].'_*.cache', 'categories');
	$db->clear_cache('subcats_'.$_POST['id'].'_*.cache', 'categories');
	$db->clear_cache('subcats_all_'.$_POST['parent'].'*.cache', 'categories');
	$db->clear_cache('subcats_all_'.$_POST['id'].'*.cache', 'categories');
	$db->clear_cache('cat_menu_list_*_'.$_POST['parent'].'_*.cache', 'categories');
	$db->clear_cache('cat_menu_list_*_'.$_POST['id'].'_*.cache', 'categories');
	$db->clear_cache('cat_menu_list_num_rows_*_'.$_POST['parent'].'_*.cache', 'categories');
	$db->clear_cache('cat_menu_list_num_rows_*_'.$_POST['id'].'_*.cache', 'categories');
	$db->clear_cache('cat_get_link_'.$_POST['parent'].'.cache', 'categories');
	$db->clear_cache('cat_get_link_'.$_POST['id'].'.cache', 'categories');
	$db->clear_cache('cat_'.$_POST['id'].'.cache', 'categories');
	$db->clear_cache('cat_'.$_POST['parent'].'.cache', 'categories');
	
	$tpl = new tpl;
	$tpl->clear_cache('*_cat-'.$_POST['id'].'*');
	$tpl->clear_cache('*_cat-'.$_POST['parent'].'*');
	//

	//redirecting to admin/categories
	redirect('index.php?module=admin&action=categories');
	exit();
	//
}

function cat_delete($x=0, $y=0) {
	if(!perms_check('categories', 'del')) {
		redirect('index.php?module=error&error=auth_error');
		exit();
	}

	global $lang, $conf;

	$id_ = $_GET['id'];
	$k = $_GET['k'];

	SQLvalidate($id_);
	SQLvalidate($k);
	SQLvalidate($x);

	if($x != 0 || $y != 0) {
		$id_ = $x;
		$k = $y;
	}

	$db = new dbquery;
	$db2 = new dbquery;
	
	$db->query("SELECT * FROM $conf[prefix]categories WHERE id = $id_") or $db->err(__FILE__, __LINE__);
	$d = $db->fetch_object();
	$ttl = 'name_'.$_SESSION['lang_short'];
	
	$x=2;

	$db->query("SELECT * FROM $conf[prefix]categories WHERE id = $id_") or $db->err(__FILE__, __LINE__);
	while($f = $db->fetch_field($x)){
		$name=$f->name;
		if(strpos($name, 'name_')!==false) break;
		$name=explode("_", $name);
		$db2->query("UPDATE $conf[prefix]".$name[0]." SET cat=0 WHERE cat = $id_") or $db->err(__FILE__, __LINE__);
		$x++;
	}

	//updating categories
	cat_update_values($id_, 0);
	//

	//deleting (sub)category from db
	$db->query("DELETE FROM $conf[prefix]categories WHERE id = $id_") or $db->err(__FILE__, __LINE__);
	//

	//adding log
	if($d->parent == 0)
	
	else
	
	//

	//czyszczenie cache
	$db->clear_cache('subcats_'.$_POST['parent'].'_*.cache', 'categories');
	$db->clear_cache('subcats_'.$_POST['id'].'_*.cache', 'categories');
	$db->clear_cache('subcats_'.$d->parent.'_*.cache', 'categories');
	$db->clear_cache('subcats_all_'.$_POST['parent'].'.cache', 'categories');
	$db->clear_cache('subcats_all_'.$_POST['id'].'.cache', 'categories');
	$db->clear_cache('subcats_all_'.$d->parent.'.cache', 'categories');
	$db->clear_cache('cat_menu_list_*_'.$_POST['parent'].'_*.cache', 'categories');
	$db->clear_cache('cat_menu_list_*_'.$_POST['id'].'_*.cache', 'categories');
	$db->clear_cache('cat_menu_list_*_'.$d->parent.'_*.cache', 'categories');
	$db->clear_cache('cat_menu_list_num_rows_*_'.$_POST['parent'].'_*.cache', 'categories');
	$db->clear_cache('cat_menu_list_num_rows_*_'.$_POST['id'].'_*.cache', 'categories');
	$db->clear_cache('cat_menu_list_num_rows_*_'.$d->parent.'_*.cache', 'categories');
	$db->clear_cache('cat_get_link_'.$_POST['parent'].'.cache', 'categories');
	$db->clear_cache('cat_get_link_'.$_POST['id'].'.cache', 'categories');
	$db->clear_cache('cat_get_link_'.$d->parent.'.cache', 'categories');
	$db->clear_cache('cat_'.$_POST['id'].'.cache', 'categories');
	$db->clear_cache('cat_'.$_POST['parent'].'.cache', 'categories');
	$db->clear_cache('cat_'.$d->parent.'.cache', 'categories');
	//

	//deleting all subcategories in cat. $id_
	$db->query("SELECT id FROM $conf[prefix]categories WHERE parent = $id_") or $db->err(__FILE__, __LINE__);

	if($db->num_rows() != 0) {
		while($n = $db->fetch_array()) {
			cat_delete($n['id'], $k);
		}
	}
	//

	//redirecting to admin/categories
	if($id_ == $k) {
		redirect('index.php?module=admin&action=categories');
		exit();
	}
}
?>
